package com.tongtech.system.controller;

import com.tongtech.common.core.utils.CryptoUtil;
import com.tongtech.common.core.utils.StringUtils;
import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.core.web.page.TableDataInfo;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.Information;
import com.tongtech.system.service.IInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 资讯信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/information")
public class InformationController extends BaseController
{
    @Autowired
    private IInformationService infoCategoryService;

    /**
     * 获取资讯列表(客户端用)
     */
    @RequiresPermissions("customer:real-time-info:list")
    @GetMapping("/client/list")
    public TableDataInfo clientList(Information infoCategory)
    {
        startPage();
        List<Information> infoCategorys = infoCategoryService.selectInformationListClient(infoCategory);
        return getDataTable(infoCategorys);
    }

    /**
     * 获取资讯列表
     */
    @RequiresPermissions("customer:real-time-info:list")
    @GetMapping("/list")
    public TableDataInfo list(Information infoCategory)
    {
        startPage();
        List<Information> infoCategorys = infoCategoryService.selectInformationList(infoCategory);
        return getDataTable(infoCategorys);
    }

    /**
     * 根据资讯编号获取详细信息
     */
    @RequiresPermissions("customer:real-time-info:query")
    @GetMapping(value = "/{infoCategoryId}")
    public AjaxResult getInfo(@PathVariable Long infoCategoryId)
    {
        return success(infoCategoryService.selectInformationById(infoCategoryId));
    }

    /**
     * 根据资讯编号获取详细信息
     */
    @RequiresPermissions("customer:real-time-info:query")
    @PostMapping(value = "/getInfo")
    public AjaxResult getInfoByPost(@RequestBody Information info)
    {
        String informationIdStr = info.getInformationIdStr();
        if(StringUtils.isNotEmpty(informationIdStr)){
            informationIdStr = CryptoUtil.decrypt(informationIdStr);
            Long infoCategoryId = Long.valueOf(informationIdStr);
            return success(infoCategoryService.selectInformationById(infoCategoryId));
        }else{
            return error("资讯编号不能为空！");
        }
    }


    /**
     * 新增资讯
     */
    @RequiresPermissions("customer:real-time-info:add")
    @Log(title = "资讯管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Information infoCategory)
    {
        infoCategory.setCreateBy(SecurityUtils.getUsername());
        return toAjax(infoCategoryService.insertInformation(infoCategory));
    }

    /**
     * 修改资讯
     */
    @RequiresPermissions("customer:real-time-info:edit")
    @Log(title = "资讯管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Information info)
    {
        info.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(infoCategoryService.updateInformation(info));
    }

    /**
     * 资讯状态修改
     */
    @RequiresPermissions("customer:real-time-info:status")
    @Log(title = "资讯管理", businessType = BusinessType.UPDATE)
    @PostMapping("/status")
    public AjaxResult editStatus(@Validated @RequestBody Information info)
    {
        info.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(infoCategoryService.updateInformation(info));
    }

    /**
     * 删除资讯
     */
    @RequiresPermissions("customer:real-time-info:remove")
    @Log(title = "资讯管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoId}")
    public AjaxResult remove(@PathVariable Long infoId)
    {
        return toAjax(infoCategoryService.deleteInformationById(infoId));
    }
}
